<template>
  <div>
    <div class="register-container">
      <!-- 顶部logo和标题 -->
      <div class="register-header">
        <el-image class="military-badge" src="" fit="cover" />
        <h1>戎归职途</h1>
        <p>贵阳贵安退役军人及随军家属就业服务平台</p>
      </div>

      <!-- Element Plus 注册表单 -->
      <el-form
        ref="registerForm"
        :model="form"
        :rules="rules"
        class="register-form"
        @submit.prevent="handleSubmit"
      >
        <el-form-item prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入邮箱"
            prefix-icon="el-icon-message"
            clearable
          />
          <div class="input-hint">
            注册Email将成为登录的账号名，推荐126、163邮箱
          </div>
        </el-form-item>

        <el-form-item prop="realName">
          <el-input
            v-model="form.realName"
            placeholder="请输入真实姓名"
            prefix-icon="el-icon-user"
            clearable
          />
        </el-form-item>

        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
            show-password
            clearable
          />
          <div class="input-hint">
            请输入8-16位同时包含大小写字母和数字的密码
          </div>
        </el-form-item>

        <el-form-item prop="confirmPassword">
          <el-input
            v-model="form.confirmPassword"
            type="password"
            placeholder="请再次输入密码"
            prefix-icon="el-icon-lock"
            show-password
            clearable
          />
        </el-form-item>

        <el-form-item prop="captcha" class="captcha-item">
          <el-input
            v-model="form.captcha"
            placeholder="请输入验证码"
            prefix-icon="el-icon-key"
            clearable
          >
            <template #append>
              <div class="captcha-box" @click="refreshCaptcha">
                <span v-if="!captchaImage">点击显示验证码</span>
                <img v-else src="https://www.gzrc.com.cn/personServe/checkcode.php" alt="验证码" />
                <el-link type="primary" :underline="false">换一张</el-link>
              </div>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item prop="agreement">
          <el-checkbox v-model="form.agreement">
            我已阅读并接受<el-link type="primary" :underline="false"
              >用户服务协议</el-link
            >
          </el-checkbox>
        </el-form-item>

        <el-form-item>
          <el-button
            class="register-btn"
            type="primary"
            native-type="submit"
            :loading="loading"
          >
            马上注册
          </el-button>
        </el-form-item>
      </el-form>

      <div class="login-link">
        已有账号?
        <el-link type="primary" :underline="false" @click="goToLogin"
          >立即登录</el-link
        >
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { ElMessage } from "element-plus";

export default {
  name: "RegisterPage",
  setup() {
    const router = useRouter();
    const registerForm = ref(null);
    const loading = ref(false);
    const captchaImage = ref(null);

    const form = ref({
      email: "",
      realName: "",
      password: "",
      confirmPassword: "",
      captcha: "",
      agreement: false,
    });

    const validatePassword = (rule, value, callback) => {
      if (!value) {
        callback(new Error("请输入密码"));
      } else if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[^]{8,16}$/.test(value)) {
        callback(new Error("密码需8-16位且包含大小写字母和数字"));
      } else {
        callback();
      }
    };

    const validateConfirmPassword = (rule, value, callback) => {
      if (value !== form.value.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };

    const rules = ref({
      email: [
        { required: true, message: "请输入邮箱", trigger: "blur" },
        {
          type: "email",
          message: "请输入正确的邮箱地址",
          trigger: ["blur", "change"],
        },
      ],
      realName: [
        { required: true, message: "请输入真实姓名", trigger: "blur" },
        { min: 2, max: 10, message: "长度在2到10个字符", trigger: "blur" },
      ],
      password: [
        { required: true, validator: validatePassword, trigger: "blur" },
      ],
      confirmPassword: [
        { required: true, validator: validateConfirmPassword, trigger: "blur" },
      ],
      captcha: [{ required: true, message: "请输入验证码", trigger: "blur" }],
      agreement: [
        {
          validator: (rule, value, callback) => {
            if (!value) {
              callback(new Error("请同意用户服务协议"));
            } else {
              callback();
            }
          },
          trigger: "change",
        },
      ],
    });

    const refreshCaptcha = () => {
      // 这里应该是获取验证码图片的API调用
      // 模拟获取验证码
      captchaImage.value =
        "";
    };

    const handleSubmit = () => {
      registerForm.value.validate((valid) => {
        if (valid) {
          loading.value = true;
          // 这里替换为实际的注册API调用
          setTimeout(() => {
            ElMessage.success("注册成功");
            router.push("/login");
            loading.value = false;
          }, 1500);
        }
      });
    };

    const goToLogin = () => {
      router.push("/login/index");
    };

    return {
      registerForm,
      form,
      rules,
      loading,
      captchaImage,
      refreshCaptcha,
      handleSubmit,
      goToLogin,
    };
  },
};
</script>

<style scoped>
.register-container {
  min-height: 100vh;
  background-color: #f8f8f8;
  display: flex;
  flex-direction: column;
  padding: 0 20px;
}

.register-header {
  text-align: center;
  margin: 40px 0 30px;
}

.military-badge {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 3px solid #d4af37;
  padding: 5px;
  background-color: white;
  margin: 0 auto;
}

.register-header h1 {
  color: #4a6b2a;
  font-size: 24px;
  font-weight: bold;
  margin: 15px 0 5px;
}

.register-header p {
  color: #666;
  font-size: 14px;
}

.register-form {
  background-color: white;
  padding: 25px 20px;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.input-hint {
  font-size: 12px;
  color: #999;
  margin-top: 5px;
}

.captcha-item :deep(.el-input-group__append) {
  padding: 0;
  background: none;
  border: none;
}

.captcha-box {
  display: flex;
  align-items: center;
  padding: 0 10px;
  cursor: pointer;
  height: 100%;
}

.captcha-box img {
  height: 30px;
  margin-right: 5px;
}

.register-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  background: linear-gradient(to right, #4a6b2a, #2c3e1a);
  border: none;
}

.register-btn:hover {
  opacity: 0.9;
}

.login-link {
  text-align: center;
  margin-top: 20px;
  color: #666;
  font-size: 14px;
}

/* 响应式调整 */
@media (max-width: 375px) {
  .register-header {
    margin: 30px 0 20px;
  }

  .military-badge {
    width: 70px;
    height: 70px;
  }

  .register-form {
    padding: 20px 15px;
  }
}
</style>